{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import importlib.resources\n",
    "import sys\n",
    "from pathlib import Path\n",
    "\n",
    "sys.path.append(str(Path.cwd().parent))\n",
    "\n",
    "from symspellpy import SymSpell, Verbosity\n",
    "from symspellpy.editdistance import DistanceAlgorithm, EditDistance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bigram_path = importlib.resources.files(\"symspellpy\") / \"frequency_bigramdictionary_en_243_342.txt\"\n",
    "\n",
    "dictionary_path = importlib.resources.files(\"symspellpy\") /  \"frequency_dictionary_en_82_765.txt\"\n",
    "\n",
    "sym_spell_damerau_osa = SymSpell(distance_comparer=EditDistance(DistanceAlgorithm.DAMERAU_OSA))\n",
    "sym_spell_damerau_osa.load_bigram_dictionary(bigram_path, 0, 2)\n",
    "sym_spell_damerau_osa.load_dictionary(dictionary_path, 0, 1)\n",
    "\n",
    "sym_spell_damerau_osa_fast = SymSpell(distance_comparer=EditDistance(DistanceAlgorithm.DAMERAU_OSA_FAST))\n",
    "sym_spell_damerau_osa_fast.load_bigram_dictionary(bigram_path, 0, 2)\n",
    "sym_spell_damerau_osa_fast.load_dictionary(dictionary_path, 0, 1)\n",
    "\n",
    "sym_spell_levenshtein = SymSpell(distance_comparer=EditDistance(DistanceAlgorithm.LEVENSHTEIN))\n",
    "sym_spell_levenshtein.load_bigram_dictionary(bigram_path, 0, 2)\n",
    "sym_spell_levenshtein.load_dictionary(dictionary_path, 0, 1)\n",
    "\n",
    "sym_spell_levenshtein_fast = SymSpell(distance_comparer=EditDistance(DistanceAlgorithm.LEVENSHTEIN_FAST))\n",
    "sym_spell_levenshtein_fast.load_bigram_dictionary(bigram_path, 0, 2)\n",
    "sym_spell_levenshtein_fast.load_dictionary(dictionary_path, 0, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def lookup_damerau_osa():\n",
    "    sym_spell_damerau_osa.lookup(\"tepmperamet\", Verbosity.ALL)\n",
    "\n",
    "def lookup_damerau_osa_fast():\n",
    "    sym_spell_damerau_osa_fast.lookup(\"tepmperamet\", Verbosity.ALL)\n",
    "\n",
    "def lookup_levenshtein():\n",
    "    sym_spell_levenshtein.lookup(\"tepmperamet\", Verbosity.ALL)\n",
    "\n",
    "def lookup_levenshtein_fast():\n",
    "    sym_spell_levenshtein_fast.lookup(\"tepmperamet\", Verbosity.ALL)\n",
    "\n",
    "def lookup_compound_damerau_osa():\n",
    "    sym_spell_damerau_osa.lookup_compound(\"whereis th elove hehad dated forImuch of thepast who couqdn'tread in sixthgrade and ins pired him\", 2)\n",
    "\n",
    "def lookup_compound_damerau_osa_fast():\n",
    "    sym_spell_damerau_osa_fast.lookup_compound(\"whereis th elove hehad dated forImuch of thepast who couqdn'tread in sixthgrade and ins pired him\", 2)\n",
    "\n",
    "def lookup_compound_levenshtein():\n",
    "    sym_spell_levenshtein.lookup_compound(\"whereis th elove hehad dated forImuch of thepast who couqdn'tread in sixthgrade and ins pired him\", 2)\n",
    "\n",
    "def lookup_compound_levenshtein_fast():\n",
    "    sym_spell_levenshtein_fast.lookup_compound(\"whereis th elove hehad dated forImuch of thepast who couqdn'tread in sixthgrade and ins pired him\", 2)\n",
    "\n",
    "def word_segmentation_damerau_osa():\n",
    "    sym_spell_damerau_osa.word_segmentation(\"thequickbrownfoxjumpsoverthelazydog\", 0)\n",
    "\n",
    "def word_segmentation_damerau_osa_fast():\n",
    "    sym_spell_damerau_osa_fast.word_segmentation(\"thequickbrownfoxjumpsoverthelazydog\", 0)\n",
    "\n",
    "def word_segmentation_levenshtein():\n",
    "    sym_spell_levenshtein.word_segmentation(\"thequickbrownfoxjumpsoverthelazydog\", 0)\n",
    "\n",
    "def word_segmentation_levenshtein_fast():\n",
    "    sym_spell_levenshtein_fast.word_segmentation(\"thequickbrownfoxjumpsoverthelazydog\", 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "107 μs ± 356 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n",
      "67.6 μs ± 319 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n",
      "95.4 μs ± 563 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n",
      "66.7 μs ± 295 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit lookup_damerau_osa()\n",
    "%timeit lookup_damerau_osa_fast()\n",
    "%timeit lookup_levenshtein()\n",
    "%timeit lookup_levenshtein_fast()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9.89 ms ± 65.3 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
      "5.1 ms ± 13.1 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
      "8.68 ms ± 46.6 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
      "4.95 ms ± 13.2 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit lookup_compound_damerau_osa()\n",
    "%timeit lookup_compound_damerau_osa_fast()\n",
    "%timeit lookup_compound_levenshtein()\n",
    "%timeit lookup_compound_levenshtein_fast()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.13 ms ± 1.36 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n",
      "1.14 ms ± 2.94 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n",
      "1.14 ms ± 3.56 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n",
      "1.14 ms ± 1.6 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit word_segmentation_damerau_osa()\n",
    "%timeit word_segmentation_damerau_osa_fast()\n",
    "%timeit word_segmentation_levenshtein()\n",
    "%timeit word_segmentation_levenshtein_fast()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Note**: Result for `word_segmentation` is expected since we are passing `max_edit_distance=0`."
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "d83327bb218665ef1f16f1956a0b9fb217f4e8f6e80f84663e37ea0a49e5699a"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
